import { Body, Controller, Delete, Get, Post, Put, Query } from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'

import { ApiResult } from '~/common/decorators/api-result.decorator'
import { ApiSecurityAuth } from '~/common/decorators/swagger.decorator'
import { DeleteDto } from '~/common/dto/delete.dto'
import { BusinessException } from '~/common/exceptions/biz.exception'
import { CreatorPipe } from '~/common/pipes/creator.pipe'
import { UpdaterPipe } from '~/common/pipes/updater.pipe'
import { ErrorEnum } from '~/constants/error-code.constant'
import { Pagination } from '~/helper/paginate/pagination'
import { definePermission, Perm } from '~/modules/system/auth/decorators/permission.decorator'
import { ParamConfigEntity } from '~/modules/system/param-config/param-config.entity'

import { ParamConfigQueryDto, ParamConfigSaveDto } from './param-config.dto'
import { ParamConfigService } from './param-config.service'

export const permissions = definePermission('system:param-config', {
  PAGE: 'page',
  LIST: 'list',
  QUERY: 'query',
  CREATE: 'create',
  UPDATE: 'update',
  DELETE: 'delete',
} as const)

@ApiTags('System - 参数配置模块')
@ApiSecurityAuth()
@Controller('param-config')
export class ParamConfigController {
  constructor(private paramConfigService: ParamConfigService) {}

  @Get('page')
  @ApiOperation({ summary: '获取参数配置分页列表' })
  @ApiResult({ type: [ParamConfigEntity], isPage: true })
  @Perm(permissions.PAGE)
  async page(@Query() dto: ParamConfigQueryDto): Promise<Pagination<ParamConfigEntity>> {
    return this.paramConfigService.page(dto)
  }

  @Get('simple-list')
  @ApiOperation({ summary: '获取参数配置列表' })
  @ApiResult({ type: [ParamConfigEntity] })
  @Perm(permissions.LIST)
  async list(@Query() dto: ParamConfigQueryDto): Promise<ParamConfigEntity[]> {
    return this.paramConfigService.list(dto)
  }

  @Get('get')
  @ApiOperation({ summary: '查询参数配置详情' })
  @ApiResult({ type: ParamConfigEntity })
  @Perm(permissions.QUERY)
  async get(@Query('id') id: number): Promise<ParamConfigEntity> {
    return this.paramConfigService.findOne(id)
  }

  @Post('create')
  @ApiOperation({ summary: '创建参数配置' })
  @Perm(permissions.CREATE)
  async create(@Body(CreatorPipe) dto: ParamConfigSaveDto): Promise<void> {
    await this.paramConfigService.create(dto)
  }

  @Put('update')
  @ApiOperation({ summary: '更新参数配置' })
  @Perm(permissions.UPDATE)
  async update(@Body(UpdaterPipe) dto: ParamConfigSaveDto): Promise<void> {
    if (!dto.id) {
      throw new BusinessException(ErrorEnum.REQUESTED_RESOURCE_NOT_FOUND)
    }
    await this.paramConfigService.update(dto.id, dto)
  }

  @Delete('delete')
  @ApiOperation({ summary: '删除参数配置' })
  @Perm(permissions.DELETE)
  async delete(@Query() dto: DeleteDto): Promise<void> {
    // 解析ids参数，支持单个和多个删除
    const ids = dto.ids.split(',').map(id => Number.parseInt(id.trim())).filter(id => !Number.isNaN(id))

    if (ids.length === 0) {
      throw new BusinessException(ErrorEnum.REQUESTED_RESOURCE_NOT_FOUND)
    }

    // 批量删除
    for (const id of ids) {
      await this.paramConfigService.delete(id)
    }
  }
}
